package com.fp.voiceshoper.receivers;

import java.util.ArrayList;
import java.util.List;

import com.fp.voiceshoper.db.DbOpenHelper;
import com.fp.voiceshoper.items.ShopListItem;
import com.fp.voiceshoper.utils.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmsManager extends BroadcastReceiver {

	List<ShopListItem> curItems=new ArrayList<ShopListItem>(0);
	
	
	AlarmManagerBroadcastReceiver tmpAlarmManagerBroadcastReceiver=new AlarmManagerBroadcastReceiver();
	private DbOpenHelper dbOpenHelper;
	
	public void updateAlarms(List<ShopListItem> items,Context context){
		
		for (int i=0;i<curItems.size();i++){
			if (curItems.get(i).getAlarm()>0){
				tmpAlarmManagerBroadcastReceiver.CancelAlarm(context,i+1);
			}
		}
		
		curItems.clear();
	
		
		for (int i=0;i<items.size();i++){
			if (items.get(i).getAlarm()>0){
				tmpAlarmManagerBroadcastReceiver.setOnetimeTimer(context, items.get(i).getAlarm(),i+1,items.get(i).getName());
				Log.v("SA1", items.get(i).getName());
			}
			curItems.add(items.get(i));
			Log.v("SA", items.get(i).getName());
		   }
	}
	
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub

		   dbOpenHelper= new DbOpenHelper(context);
		   curItems=dbOpenHelper.getLists();
		   updateAlarms(dbOpenHelper.getLists(),context);
//			Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();

	}
	
	

}
